package top.ztf.erp.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import top.ztf.erp.dao.RoleDao;
import top.ztf.erp.dao.RolePermissionRelationDao;
import top.ztf.erp.dao.UserRoleDao;
import top.ztf.erp.domain.Permission;
import top.ztf.erp.domain.Role;
import top.ztf.erp.service.RoleService;
import top.ztf.erp.utils.MyBatisUtil;
import top.ztf.erp.utils.TimeUtil;
import top.ztf.erp.vo.PermissionVo;

import java.lang.reflect.Array;
import java.util.*;

public class RoleServiceImp implements RoleService {
    @Override
    public PageInfo<Role> getRolePage(String pageNo, String pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDao mapper = sqlSession.getMapper(RoleDao.class);
        PageHelper.startPage(Optional.ofNullable(pageNo).
                                filter(StringUtils::isNumeric).filter(t->!"".equals(t)).
                                map(Integer::parseInt).
                                orElse(1),
                            Optional.ofNullable(pageSize).
                                filter(StringUtils::isNumeric).filter(t->!"".equals(t)).
                                map(Integer::parseInt).
                                orElse(10));//执行sql的语句,必须紧挨此方法调用之后
        List<Role> rolePage = mapper.getRolePage();
        PageInfo<Role> rolePageInfo = new PageInfo<>(rolePage);
        sqlSession.clearCache();//
        return rolePageInfo;
    }

    @Override
    public Role getRoleById(String id) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDao mapper = sqlSession.getMapper(RoleDao.class);
        Role role = mapper.selectRoleById(id);
        return role;
    }

    @Override
    public int save(Role role) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        role.setCreatTime(TimeUtil.getNowTime());
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        try {
            int save = roleDao.save(role);
            sqlSession.commit();
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 0;
        }

    }

    @Override
    public List<PermissionVo> getPermissionByRole(String roleId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        List<Permission> permissionList = roleDao.getPermissionByRole(roleId);
        List<PermissionVo> list = new ArrayList<>(permissionList.size());
        permissionList.forEach(t->list.add(t.clone(PermissionVo.class)));
        return list;
    }

    @Override
    public List<Map<String, Object>> getAssignPermission(String roleId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        List<Map<String, Object>> permission = roleDao.getAssignPermission(roleId);
        for (Map<String, Object> map : permission) {
            boolean checked =  StringUtils.isEmpty(String.valueOf(map.get("checked")));
            map.put("checked",!checked);
            map.put("open",true);
        }
        return permission;
    }

    @Override
    public int updatePermissionByRole(String roleId, String[] arrays) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RolePermissionRelationDao relationDao = sqlSession.getMapper(RolePermissionRelationDao.class);
        try {
            int delCount = relationDao.deleteRelationByRole(roleId);
            if (arrays!=null){
                int insertCount = relationDao.insertRelation(arrays,roleId);
            }
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteRole(String[] arrays) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();

        // 查看该角色是否有用户使用
        UserRoleDao userRoleDao = sqlSession.getMapper(UserRoleDao.class);
        int count = userRoleDao.selectRoleIsUseing(arrays);
        if (count!=0){
            return 0;
        }
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        RolePermissionRelationDao relationDao = sqlSession.getMapper(RolePermissionRelationDao.class);
        try {
           roleDao.deleteRole(arrays);
           relationDao.deleteRelation(arrays);
           sqlSession.commit();
           return 1;
       }catch (Exception e){
           e.printStackTrace();
           sqlSession.rollback();
           return 0;
       }

    }

    @Override
    public int update(Role role) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        role.setEditTime(TimeUtil.getNowTime());
        try {
            int count = roleDao.update(role);
            sqlSession.commit();
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 0;
        }
    }

    @Override
    public int selectRoleByName(String id, String name) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        int count =  roleDao.selectRoleByName(id,name);
        return count;
    }
}
